/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.ucue.ptc.rest;

import edu.ucue.ptc.gco.mdl.Empleado;
import edu.ucue.ptc.gco.ngc.EmpleadoManagerLocal;
import edu.ucue.ptc.gco.ngc.UsuarioManagerLocal;
import edu.ucue.ptc.gco.util.AtributoInvalidoException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.PathParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;

/**
 * REST Web Service
 *
 * @author Valex
 */
@Path("empleado/{nombre}/{clave}/{callback}")
@RequestScoped
public class EmpleadoResource {

    @Context
    private UriInfo context;

     @Inject
    private UsuarioManagerLocal UsuarioManager;
    
    @Inject
    private EmpleadoManagerLocal EmpleadoManager;
    
    /**
     * Creates a new instance of EmpleadoResource
     */
    public EmpleadoResource() {
    }

    /**
     * Retrieves representation of an instance of edu.ucue.ptc.rest.EmpleadoResource
     * @return an instance of java.lang.String
     */
    @GET
    @Named
    @Produces("application/json")
    public String getJson(@PathParam("callback") String callback,@PathParam("nombre") String user, 
    @PathParam("clave") String pass) {
        //TODO return proper representation object
       // throw new UnsupportedOperationException();
        int IdPersona = UsuarioManager.verificarUsuario(user, pass);
        //String a=",\"valor\":\""+verificarUsuario;
        
        if(IdPersona!=-1)
        {
            try {
                Empleado empleado = EmpleadoManager.buscarEmpleadoPorId(IdPersona, user, pass);
                
                if(empleado==null)
                {
                    return callback+"({\"callback\":\""+callback+"\",\"errormsg\":\""+"Usted no esta habilitado para esta aplicación"+"\"})";
                }
                else
                    return callback+"({\"callback\":\""+callback+"\",\"id\":\""+empleado.getIdPersona()+"\",\"ci\":\""+empleado.getCedulaRuc()+
                        "\",\"nombre\":\""+empleado.getNombres()+"\",\"apellido\":\""+empleado.getApellidos()+"\",\"cargo\":\""+empleado.getCargo().getNombre()+"\"})";
            } catch (AtributoInvalidoException ex) {
                Logger.getLogger(RutaResource.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
            //buscarEmpleadoPorId
        return "({\"callback\":\""+callback+"\",\"errormsg\":\""+"Usuario no registrado"+"\"})";
    }

    /**
     * PUT method for updating or creating an instance of EmpleadoResource
     * @param content representation for the resource
     * @return an HTTP response with content of the updated or created resource.
     */
    @PUT
    @Consumes("application/json")
    public void putJson(String content) {
    }
}
